/*
* @Author: ocn27
* @Date:   2017-08-07 14:38:18
* @Last Modified by:   Administrator
* @Last Modified time: 2017-08-16 10:02:07
*/

'use strict';
var webpack             = require('webpack');
var ExtractTextPlugin   = require("extract-text-webpack-plugin"); // webpack css打包
var HtmlWebpackPlugin   = require('html-webpack-plugin'); // webpack html

// 环境变量配置， dev / online
var WEBPACK_ENV         = process.env.WEBPACK_ENV || 'dev';
console.log(WEBPACK_ENV);

// 获取html-webpack-plugin参数的方法
var getHtmlConfig = function(name, title){
    var projectName = '约课平台';
    return {
        template    : 'src/view/' + name + '.html',
        filename    : 'view/' + name + '.html',
        favicon     : './favicon.ico',
        title       : title,
        projectname : projectName,
        inject      : true,
        hash        : true,
        chunks      : ['common', name]
    }
}

var config = {
    entry: {
    	'common'           : ['./src/page/common/index.js'],
        'index'            : ['./src/page/index/index.js'],
        'homepage'         : ['./src/page/homepage/index.js'],
        'course-reserve'   : ['./src/page/course-reserve/index.js'],
        'timetable'        : ['./src/page/timetable/index.js'],
    	'about'            : ['./src/page/about/index.js'],
        'user-login'       : ['./src/page/user-login/index.js'],
        'user-register'    : ['./src/page/user-register/index.js'],
        'user-pwd-reset'   : ['./src/page/user-pwd-reset/index.js'],
        'user-center'      : ['./src/page/user-center/index.js'],
    	'result'           : ['./src/page/result/index.js'],
    },
    output: {
        path        : __dirname + '/dist',
        publicPath  : 'dev' === WEBPACK_ENV ? '/dist/' : '//sy.kengha.com/course-reserve/dist/',
        filename    : 'js/[name].js'
    },
    externals : {
        'jquery' : 'window.jQuery'
    },
    module: {
        loaders: [
            { test: /\.css$/, loader:  ExtractTextPlugin.extract("style-loader", "css-loader") },
            { test: /\.(gif|png|jpg|woff|svg|eot|ttf)\??.*$/, loader:  "url-loader?limit=100&name=resource/[name].[ext]" },
            { test: /\.string$/, loader:  "html-loader" }
        ]
    },
    resolve : {
        alias : {
            node_modules    : __dirname + '/node_modules',
            util            : __dirname + '/src/util',
            page            : __dirname + '/src/page',
            service         : __dirname + '/src/service',
            image           : __dirname + '/src/image',
        }
    },
    plugins : [
        // 独立通用模块到js/base.js
    	new webpack.optimize.CommonsChunkPlugin({
    		name : 'common',
    		filename : 'js/base.js'
    	}),
        // css单独打包
        new ExtractTextPlugin("css/[name].css"),
        // html模板处理
        new HtmlWebpackPlugin(getHtmlConfig('index', '首页')),
        new HtmlWebpackPlugin(getHtmlConfig('homepage', '首页')),
        new HtmlWebpackPlugin(getHtmlConfig('course-reserve', '约课')),
        new HtmlWebpackPlugin(getHtmlConfig('timetable', '课程表')),
        new HtmlWebpackPlugin(getHtmlConfig('about', '我的')),
        new HtmlWebpackPlugin(getHtmlConfig('user-login', '用户登录')),
        new HtmlWebpackPlugin(getHtmlConfig('user-register', '用户注册')),
        new HtmlWebpackPlugin(getHtmlConfig('user-pwd-reset', '找回密码')),
        new HtmlWebpackPlugin(getHtmlConfig('user-center', '个人中心')),
        new HtmlWebpackPlugin(getHtmlConfig('result', '操作结果')),
    ]
 };

if('dev' === WEBPACK_ENV){
    config.entry.common.push('webpack-dev-server/client?http://localhost:8039/');
}

module.exports = config;
